<template>
    <view class="circle-container">
        <addHeader :title="'圈子'"></addHeader>
<!--        <uniForm :data="addCircleData" @onGetData="getTeacherData"></uniForm>-->
        <view class="form-content">
            <uni-forms ref="customForm" :modelValue="formData" label-width="85px">
                <uni-forms-item label="圈子头像" name="accessory">
                    <uni-file-picker fileMediatype="image" @select="onUpload"  limit="1"></uni-file-picker>
                </uni-forms-item>
                <uni-forms-item label="圈子名称" name="title">
                    <uni-easyinput v-model="formData.title" placeholder="请输入圈子名称" />
                </uni-forms-item>
                <uni-forms-item label="学科" name="subject">
                    <uni-data-select v-model="formData.subject" :localdata="subject" placeholder="请选择学科"></uni-data-select>
                </uni-forms-item>
                <uni-forms-item label="成员所属区域" name="area">
                    <uni-data-select v-model="formData.area" @change="onChangeArea" :localdata="areaData" placeholder="请选择成员所属区域"></uni-data-select>
                </uni-forms-item>
                <uni-forms-item label="圈子成员" name="group_user">
                    <uni-data-select v-model="formData.group_user" :localdata="groupUser" placeholder="请选择圈子成员"></uni-data-select>
                </uni-forms-item>
                <uni-forms-item label="圈子内容" name="content">
                    <uni-easyinput v-model="formData.content" type="textarea" placeholder="请输入圈子内容" />
                </uni-forms-item>
                <uni-forms-item><u-button @click="onSubmitClick" type="primary">立即添加</u-button></uni-forms-item>
            </uni-forms>
        </view>
    </view>
</template>

<script>
    import config from '@/config'
    const baseUrl = config.baseUrl
    import addHeader from './add-header.vue';
    import { getTeacher } from '@/api/teach'
    export default {
        data() {
            return {
                deviceId: this.$store.state.user.deviceId,
                subject:this.$store.state.teach.subject,
                areaData:this.$store.state.teach.area,
                groupUser:[],
                formData:{
                    accessory:"",
                    title:"",
                    subject:"",
                    area:"",
                    group_user:"",
                    content:"",
                    deviceId: this.$store.state.user.deviceId,
                }
            }
        },
        components:{addHeader},
        methods: {
            onChangeArea(e){
                getTeacher({area_code:e,deviceId: this.deviceId}).then(res=>{
                    res.users.forEach(item=>{
                        this.groupUser.push({value:item.id,text:item.name})
                    })
                })
            },
            onUpload(e){
                this.formData.accessory=e.tempFilePaths[0];
            },
            onSubmitClick() {
                uni.showLoading({title: '正在保存'})
                uni.uploadFile({
                    url: baseUrl+'/circle/addCircle',
                    filePath: this.formData.accessory,
                    name: 'file',
                    formData: this.formData,
                    success: (res) => {
                        if(res.statusCode == 200){
                            uni.$u.toast('保存成功');
                            setTimeout(() => {
                                this.$tab.reLaunch('/pages/work/index')
                            }, 2000);
                        }
                    }
                });
            }
        }
    }
</script>

<style lang="scss">
    page{
        background: #ddd;
        .form-content{
            background: #fff;
            padding: 15px;
        }
    }
</style>
